package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.model.vo.Catalog2VO;
import com.atguigu.gulimall.product.service.CategoryService;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class RedissonController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redisson;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        //获取一把锁 锁名一样就是同一把锁
        RLock lock = redisson.getLock("my-lock");

        //lock.lock();//阻塞式等待 默认30S 锁自动续期
        lock.lock(10, TimeUnit.SECONDS); //不会自动续期

        try {
            System.out.println(Thread.currentThread().getName() + "加锁成功，执行业务");
            Thread.sleep(40_000);
        } catch (InterruptedException e) {
        } finally {
            lock.unlock();
            System.out.println(Thread.currentThread().getName() + "释放锁");
        }

        return "hello";
    }


    @ResponseBody
    @GetMapping("/write")
    public String write() {
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("read-write-lock");
        RLock wLock = readWriteLock.writeLock();

        String uuid = "";
        try {
            wLock.lock();
            uuid = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("uuid", uuid);
            Thread.sleep(5_000);
        } catch (InterruptedException ignored) {
        } finally {
            wLock.unlock();
        }
        return uuid;
    }


    @ResponseBody
    @GetMapping("/read")
    public String read() {
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("read-write-lock");
        RLock rLock = readWriteLock.readLock();
        String uuid = "";
        try {
            rLock.lock();
            uuid = stringRedisTemplate.opsForValue().get("uuid");
        } finally {
            rLock.unlock();
        }
        return uuid;
    }

    /**
     * 放学关门——分布式闭锁
     *
     * @return
     * @throws InterruptedException
     */
    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假了";
    }

    @ResponseBody
    @GetMapping("/go/{id}")
    @Cacheable(cacheNames = "RedissonController", key = "#root.method.name+':'+#root.args[0]")
    public String go(@PathVariable("id") Long id) {
        System.out.println("go-home");
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();//计数减一
        return id + "走了";
    }

    /**
     * 车库停车——信号量
     * <p>
     * 信号量可以用于分布式限流
     *
     * @return
     * @throws InterruptedException
     */
    @ResponseBody
    @GetMapping("/park")
    public String park() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
//        park.acquire();//获取一个信号
        boolean b = park.tryAcquire();//获取一个信号
        return "ok=>" + b;
    }

    @ResponseBody
    @GetMapping("/go")
    public String go() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        park.release();//获取一个信号
        return "ok";
    }


}
